package com.qishenyuan.competition.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

@Getter
@AllArgsConstructor
public enum CompetitionStatus {

    NOT_STARTED(1, "未开始"),
    REGISTERING(2, "报名中"),
    PREPARING(3, "准备比赛"),
    IN_PROGRESS(4, "比赛中"),
    ENDED(5, "已结束");

    private final int statusCode;
    private final String statusName;

    public static CompetitionStatus getStatus(Long registrationPeriodBegin, Long registrationPeriodEnd,
                                              Long gameBeginTime, Long gameEndTime) {

        LocalDate currentDate = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate regStart = Instant.ofEpochMilli(registrationPeriodBegin).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate regEnd = Instant.ofEpochMilli(registrationPeriodEnd).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate gameStart = Instant.ofEpochMilli(gameBeginTime).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate gameEnd = Instant.ofEpochMilli(gameEndTime).atZone(ZoneId.systemDefault()).toLocalDate();

        if (currentDate.isBefore(regStart)) {
            return CompetitionStatus.NOT_STARTED;
        } else if (!currentDate.isBefore(regStart) && !currentDate.isAfter(regEnd)) {
            return CompetitionStatus.REGISTERING;
        } else if (!currentDate.isBefore(regEnd) && currentDate.isBefore(gameStart)) {
            return CompetitionStatus.PREPARING;
        } else if (!currentDate.isBefore(gameStart) && !currentDate.isAfter(gameEnd)) {
            return CompetitionStatus.IN_PROGRESS;
        } else {
            return CompetitionStatus.ENDED;
        }
    }
}
